View Javadoc
1   package org.apache.maven.surefire.junitcore;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Map;
25  
26  import org.apache.maven.surefire.booter.ProviderParameterNames;
27  
28  /**
29   * @author Kristian Rosenvold
30   */
31  public final class JUnitCoreParameters
32  {
33      public static final String PARALLEL_KEY = ProviderParameterNames.PARALLEL_PROP;
34  
35      public static final String PERCORETHREADCOUNT_KEY = "perCoreThreadCount";
36  
37      public static final String THREADCOUNT_KEY = ProviderParameterNames.THREADCOUNT_PROP;
38  
39      public static final String THREADCOUNTSUITES_KEY = ProviderParameterNames.THREADCOUNTSUITES_PROP;
40  
41      public static final String THREADCOUNTCLASSES_KEY = ProviderParameterNames.THREADCOUNTCLASSES_PROP;
42  
43      public static final String THREADCOUNTMETHODS_KEY = ProviderParameterNames.THREADCOUNTMETHODS_PROP;
44  
45      public static final String USEUNLIMITEDTHREADS_KEY = "useUnlimitedThreads";
46  
47      public static final String PARALLEL_TIMEOUT_KEY = ProviderParameterNames.PARALLEL_TIMEOUT_PROP;
48  
49      public static final String PARALLEL_TIMEOUTFORCED_KEY = ProviderParameterNames.PARALLEL_TIMEOUTFORCED_PROP;
50  
51      public static final String PARALLEL_OPTIMIZE_KEY = ProviderParameterNames.PARALLEL_OPTIMIZE_PROP;
52  
53      private final String parallel;
54  
55      private final boolean perCoreThreadCount;
56  
57      private final int threadCount;
58  
59      private final int threadCountSuites;
60  
61      private final int threadCountClasses;
62  
63      private final int threadCountMethods;
64  
65      private final double parallelTestsTimeoutInSeconds;
66  
67      private final double parallelTestsTimeoutForcedInSeconds;
68  
69      private final boolean useUnlimitedThreads;
70  
71      private final boolean parallelOptimization;
72  
73      public JUnitCoreParameters( Map<String, String> properties )
74      {
75          parallel = property( properties, PARALLEL_KEY, "none" ).toLowerCase();
76          perCoreThreadCount = property( properties, PERCORETHREADCOUNT_KEY, true );
77          threadCount = property( properties, THREADCOUNT_KEY, 0 );
78          threadCountMethods = property( properties, THREADCOUNTMETHODS_KEY, 0 );
79          threadCountClasses = property( properties, THREADCOUNTCLASSES_KEY, 0 );
80          threadCountSuites = property( properties, THREADCOUNTSUITES_KEY, 0 );
81          useUnlimitedThreads = property( properties, USEUNLIMITEDTHREADS_KEY, false );
82          parallelTestsTimeoutInSeconds = Math.max( property( properties, PARALLEL_TIMEOUT_KEY, 0d ), 0 );
83          parallelTestsTimeoutForcedInSeconds = Math.max( property( properties, PARALLEL_TIMEOUTFORCED_KEY, 0d ), 0 );
84          parallelOptimization = property( properties, PARALLEL_OPTIMIZE_KEY, true );
85      }
86  
87      private static Collection<String> lowerCase( String... elements )
88      {
89          ArrayList<String> lowerCase = new ArrayList<String>();
90          for ( String element : elements )
91          {
92              lowerCase.add( element.toLowerCase() );
93          }
94          return lowerCase;
95      }
96  
97      private boolean isAllParallel()
98      {
99          return "all".equals( parallel );
100     }
101 
102     public boolean isParallelMethods()
103     {
104         return isAllParallel() || lowerCase( "both", "methods", "suitesAndMethods", "classesAndMethods" ).contains(
105             parallel );
106     }
107 
108     public boolean isParallelClasses()
109     {
110         return isAllParallel() || lowerCase( "both", "classes", "suitesAndClasses", "classesAndMethods" ).contains(
111             parallel );
112     }
113 
114     public boolean isParallelSuites()
115     {
116         return isAllParallel() || lowerCase( "suites", "suitesAndClasses", "suitesAndMethods" ).contains( parallel );
117     }
118 
119     /**
120      * @deprecated Instead use the expression ( {@link #isParallelMethods()} && {@link #isParallelClasses()} ).
121      */
122     @Deprecated
123     @SuppressWarnings( { "unused", "deprecation" } )
124     public boolean isParallelBoth()
125     {
126         return isParallelMethods() && isParallelClasses();
127     }
128 
129     public boolean isPerCoreThreadCount()
130     {
131         return perCoreThreadCount;
132     }
133 
134     public int getThreadCount()
135     {
136         return threadCount;
137     }
138 
139     public int getThreadCountMethods()
140     {
141         return threadCountMethods;
142     }
143 
144     public int getThreadCountClasses()
145     {
146         return threadCountClasses;
147     }
148 
149     public int getThreadCountSuites()
150     {
151         return threadCountSuites;
152     }
153 
154     public boolean isUseUnlimitedThreads()
155     {
156         return useUnlimitedThreads;
157     }
158 
159     public double getParallelTestsTimeoutInSeconds()
160     {
161         return parallelTestsTimeoutInSeconds;
162     }
163 
164     public double getParallelTestsTimeoutForcedInSeconds()
165     {
166         return parallelTestsTimeoutForcedInSeconds;
167     }
168 
169     public boolean isNoThreading()
170     {
171         return !isParallelismSelected();
172     }
173 
174     public boolean isParallelismSelected()
175     {
176         return isParallelSuites() || isParallelClasses() || isParallelMethods();
177     }
178 
179     public boolean isParallelOptimization()
180     {
181         return parallelOptimization;
182     }
183 
184     @Override
185     public String toString()
186     {
187         return "parallel='" + parallel + '\'' + ", perCoreThreadCount=" + perCoreThreadCount + ", threadCount="
188             + threadCount + ", useUnlimitedThreads=" + useUnlimitedThreads + ", threadCountSuites=" + threadCountSuites
189             + ", threadCountClasses=" + threadCountClasses + ", threadCountMethods=" + threadCountMethods
190             + ", parallelOptimization=" + parallelOptimization;
191     }
192 
193     private static boolean property( Map<String, String> properties, String key, boolean fallback )
194     {
195         return properties.containsKey( key ) ? Boolean.valueOf( properties.get( key ) ) : fallback;
196     }
197 
198     private static String property( Map<String, String> properties, String key, String fallback )
199     {
200         return properties.containsKey( key ) ? properties.get( key ) : fallback;
201     }
202 
203     private static int property( Map<String, String> properties, String key, int fallback )
204     {
205         return properties.containsKey( key ) ? Integer.valueOf( properties.get( key ) ) : fallback;
206     }
207 
208     private static double property( Map<String, String> properties, String key, double fallback )
209     {
210         return properties.containsKey( key ) ? Double.valueOf( properties.get( key ) ) : fallback;
211     }
212 }